import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  // {
  //   path: '/',
  //   redirect:'/components'
  // },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    // 路由是否公开 不公开则需要校验
    meta: { isPublic: true }
  },
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main.vue'),
    children: [
      {
        path: '/categories/create',
        component: () => import('../components/categories/create.vue'),
      },
      {
        path: '/categories/list',
        component: () => import('../components/categories/list.vue'),
      },
      {
        path: '/categories/create/:id',
        component: () => import('../components/categories/create.vue'),
        props: true
      },

      {
        path: '/items/create',
        component: () => import('../components/items/create.vue'),
      },
      {
        path: '/items/list',
        component: () => import('../components/items/list.vue'),
      },
      {
        path: '/items/create/:id',
        component: () => import('../components/items/create.vue'),
        props: true
      },

      {
        path: '/heroes/create',
        component: () => import('../components/heroes/create.vue'),
      },
      {
        path: '/heroes/list',
        component: () => import('../components/heroes/list.vue'),
      },
      {
        path: '/heroes/create/:id',
        component: () => import('../components/heroes/create.vue'),
        props: true
      },

      {
        path: '/articles/create',
        component: () => import('../components/articles/create.vue'),
      },
      {
        path: '/articles/list',
        component: () => import('../components/articles/list.vue'),
      },
      {
        path: '/articles/create/:id',
        component: () => import('../components/articles/create.vue'),
        props: true
      },

      {
        path: '/ads/create',
        component: () => import('../components/ads/create.vue'),
      },
      {
        path: '/ads/list',
        component: () => import('../components/ads/list.vue'),
      },
      {
        path: '/ads/create/:id',
        component: () => import('../components/ads/create.vue'),
        props: true
      },

      {
        path: '/admin_user/create',
        component: () => import('../components/admin_user/create.vue'),
      },
      {
        path: '/admin_user/list',
        component: () => import('../components/admin_user/list.vue'),
      },
      {
        path: '/admin_user/create/:id',
        component: () => import('../components/admin_user/create.vue'),
        props: true
      },

    ]
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 进入路由之前都会检查是否符合条件 不符合则直接跳转到登录页
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})

export default router